package com.sdap2.controller;

import com.sdap2.common.model.ApiResult;
import com.sdap2.dto.DeviceDTO;
import com.sdap2.dto.DeviceDataResponse;
import com.sdap2.entity.Device;
import com.sdap2.service.DeviceService;
import com.sdap2.vo.DeviceVO;
import com.sdap2.dto.DeviceListDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Tag(name = "设备管理")
@RestController
@RequestMapping("/api/devices")
@RequiredArgsConstructor
public class DeviceController {
    
    private final DeviceService deviceService;
    
    @Operation(summary = "创建设备")
    @PostMapping
    public ApiResult<DeviceVO> create(
            @Parameter(description = "设备信息") @Valid @RequestBody DeviceDTO dto,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        Device device = deviceService.create(dto, token);
        return ApiResult.success(DeviceVO.fromEntity(device));
    }
    
    @Operation(summary = "更新设备")
    @PutMapping("/{id}")
    public ApiResult<DeviceVO> update(
            @Parameter(description = "设备ID") @PathVariable Long id,
            @Parameter(description = "设备信息") @Valid @RequestBody DeviceDTO dto,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        Device device = deviceService.update(id, dto, token);
        return ApiResult.success(DeviceVO.fromEntity(device));
    }
    
    @Operation(summary = "删除设备")
    @DeleteMapping("/{id}")
    public ApiResult<Void> delete(
            @Parameter(description = "设备ID") @PathVariable Long id,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        deviceService.delete(id, token);
        return ApiResult.success();
    }
    
    @Operation(summary = "获取设备详情")
    @GetMapping("/{id}")
    public ApiResult<DeviceVO> getById(
            @Parameter(description = "设备ID") @PathVariable Long id,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        Device device = deviceService.getById(id, token);
        return ApiResult.success(DeviceVO.fromEntity(device));
    }
    
    @Operation(summary = "分页查询大棚下的设备")
    @GetMapping
    public ApiResult<Page<DeviceListDTO>> getPage(
            @Parameter(description = "大棚ID") @RequestParam Long greenhouseId,
            @Parameter(description = "分页参数") @PageableDefault(size = 10) Pageable pageable,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        Page<Device> page = deviceService.getPage(greenhouseId, pageable, token);
        return ApiResult.success(page.map(DeviceListDTO::fromEntity));
    }
    
    @Operation(summary = "根据设备序列号查询设备记录")
    @GetMapping("/serial/{serialNumber}/records")
    public ApiResult<Page<?>> getDeviceRecords(
            @Parameter(description = "设备序列号") @PathVariable String serialNumber,
            @Parameter(description = "开始时间") @RequestParam(required = false) LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) LocalDateTime endTime,
            @Parameter(description = "分页参数") @PageableDefault(size = 10) Pageable pageable,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        return ApiResult.success(deviceService.getDeviceRecords(serialNumber, startTime, endTime, pageable, token));
    }
    
    @Operation(summary = "根据设备序列号查询最新记录")
    @GetMapping("/serial/{serialNumber}/latest")
    public ApiResult<?> getLatestRecord(
            @Parameter(description = "设备序列号") @PathVariable String serialNumber,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        return ApiResult.success(deviceService.getLatestRecord(serialNumber, token));
    }
} 